package work.konbai.common.oss.config;

import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import lombok.AllArgsConstructor;
import work.konbai.common.oss.MinioUtils;
import work.konbai.common.oss.props.MinioProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Minio配置类
 *
 * @author KonBAI
 * @version 1.0
 * Create by 2021/2/25 9:28
 */
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties({MinioProperties.class})
public class MinioAutoConfiguration {

	private final MinioProperties properties;

	@Bean
	public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {
		return new MinioClient(properties.getEndpoint(), properties.getAccessKey(), properties.getSecretKey());
	}

	@Bean
	@ConditionalOnMissingBean(MinioUtils.class)
	@ConditionalOnBean({MinioClient.class})
	public MinioUtils minioUtils(MinioClient minioClient) {
		return new MinioUtils(minioClient);
	}
}
